package com.xuecheng.content.controller;

import com.xuecheng.api.content.model.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import com.xuecheng.content.convert.TeachplanMediaConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;

    //todo 根据课程id查询课程计划
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodes(@PathVariable("courseId") Long courseId) {
        //todo SecurityUtil对前端传入的请求数据进行了封装,getCompanyId获取每个请求的独有id,然后传入后端获取独有的数据,从而实现数据分隔,保护彼此数据安全性
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO teachplanDTO = teachplanService.queryTreeNodes(courseId, companyId);
        return teachplanDTO;
    }

    //todo  课程计划增加和修改
    @PostMapping("/teachplan")
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO vo) {
        TeachplanDTO teachplanDTO = TeachplanConvert.INSTANCE.vo2dto(vo);
        //todo SecurityUtil对前端传入的请求数据进行了封装,getCompanyId获取每个请求的独有id,然后传入后端获取独有的数据,从而实现数据分隔,保护彼此数据安全性
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO orModifyTeachPlan= teachplanService.createOrModifyTeachPlan(teachplanDTO, companyId);

        return orModifyTeachPlan;
    }

    //todo 课程删除
    @DeleteMapping("/teachplan/{teachPlanId}")
    public void removeTeachPlan(@PathVariable Long teachPlanId) {
        //todo SecurityUtil对前端传入的请求数据进行了封装,getCompanyId获取每个请求的独有id,然后传入后端获取独有的数据,从而实现数据分隔,保护彼此数据安全性
        Long companyId = SecurityUtil.getCompanyId();
        teachplanService.removeTeachPlan(teachPlanId,companyId);
    }

@PostMapping("/teachplan/media/association")
    public TeachplanMediaDTO associationTeachplanMedia(@RequestBody BindTeachplanMediaVO vo) {
    TeachplanMediaDTO teachplanMediaDTO = TeachplanMediaConvert.INSTANCE.vo2dto(vo);
    //todo SecurityUtil对前端传入的请求数据进行了封装,getCompanyId获取每个请求的独有id,然后传入后端获取独有的数据,从而实现数据分隔,保护彼此数据安全性
    Long companyId = SecurityUtil.getCompanyId();
    teachplanService.associateMedia(teachplanMediaDTO,companyId);
    return null;
    }
}
